<template>
<div>
  <div>
  <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
    <el-breadcrumb-item :to="{ path: '/admin/index' }">
      <i class="el-icon-s-promotion"></i>
      管理后台首页
    </el-breadcrumb-item>
    <el-breadcrumb-item :to="{path: '/admin/ill/list'}">病例信息</el-breadcrumb-item>
    <el-breadcrumb-item>病例信息修改</el-breadcrumb-item>
  </el-breadcrumb>
  <el-divider></el-divider>

    <el-card class="box-card">
      <h1 style="font-size: 25px;margin-bottom: 25px;text-align: center">病例信息修改</h1>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="老人姓名:" prop="olderId">
          <template>
            <el-select v-model="ruleForm.olderId" placeholder="请选择">
              <el-option
                  v-for="item in olderListOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="药品名称:" prop="medicineId">
          <template>
            <el-select v-model="ruleForm.medicineId" placeholder="请选择">
              <el-option
                  v-for="item in medicineListOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="疾病名称:" prop="name">
          <el-input v-model="ruleForm.name" ></el-input>
        </el-form-item>
        <el-form-item label="疾病类型:" prop="type">
          <el-input v-model="ruleForm.type"></el-input>
        </el-form-item>
        <el-form-item label="治愈方法:" prop="cure">
          <el-input v-model="ruleForm.cure"></el-input>
        </el-form-item>
        <el-form-item label="最近用药时间:" prop="medicineTime">
          <el-date-picker
              v-model="ruleForm.medicineTime"
              type="datetime"
              placeholder="选择日期" >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="病情详细:" prop="info">
          <el-input v-model="ruleForm.info" ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">修改</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</div>
</template>

<script>
import axios from "axios";

export default {
  data() {
    return {
      olderListOptions: [],
      medicineListOptions: [],
      ruleForm: {
        olderId: '',
        olderName: '',
        medicineId: '',
        medicineName: '',
        id: '',
        name: '测试病例名称001',
        type: '痔疮',
        cure: '割了',
        medicineTime: '',
        info: '流血'
      },
      rules: {
        name: [
          { required: true, message: '请选择老人信息', trigger: 'blur' }
        ],
        date:[
          { required: true, message: '请选择入住时间', trigger: 'blur' }
        ]
      },
       tableShow:false,
      Content: "",
      tableData: [],
      currentRow: null
    };
  },
  methods: {
    loadLocalRuleForm() {
      let localRuleFormString = localStorage.getItem('localRuleForm');
      let localRuleForm = JSON.parse(localRuleFormString);
      this.ruleForm = localRuleForm;
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:8080/ills/'+location.search.split("=")[1]+'/update';
          console.log('url = ' + url);

          let formData = this.qs.stringify(this.ruleForm);
          console.log('formData = ' + formData);

          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt') }})
              .post(url, this.ruleForm).then((response)=>{
            if(response.data.state==20000)
            {
              this.$message.success("修改成功！");
            location.href= "/admin/ill/list";
            }
            else {
              this.$message.error(response.data.message);
            }
          })

        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    loadOlderList() {
      let url = "http://localhost:8080/olders";
      console.log("url = " + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt') }})
          .get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.olderListOptions = responseBody.data;
          this.ruleForm.olderName = responseBody.data.name;
          localStorage.setItem('localRuleForm', JSON.stringify(this.ruleForm));
        }
      });
    },
    loadMedicineList() {
      let url = "http://localhost:8080/medicines";
      console.log("url = " + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt') }})
          .get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.medicineListOptions = responseBody.data;
          this.ruleForm.medicineName = responseBody.date.name;
          localStorage.setItem('localRuleForm', JSON.stringify(this.ruleForm));
        }
      });
    },
    loadIllList() {
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt') }})
          .get("http://localhost:8080/ills/"+location.search.split("=")[1])
          .then((response)=>
          {
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              this.ruleForm = responseBody.data;
            }
          })
    }
  },
  mounted() {
    this.loadOlderList();
    this.loadMedicineList();
    this.loadIllList();
    this.loadLocalRuleForm();
  }

}
</script>

<style>
.box-card{
  width: 80%;
  height: 800px;
  margin: 0 auto;
}
.demo-ruleForm{
  width: 50%;
  margin: 0 auto;
}

.el-input {
  width: 500px;
}
</style>